import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // void _incrementCounter() {
  //   setState(() {
  //     _counter++;
  //   });
  // }

  /// 从 DevTool 中可见，其跟底部的 Scaffold 不是一个层级
  /// 通过其实现看，其内部也是使用了 Navigator 来实现跳转到一个新页面。
  void _showModalBottomSheet() {
    var builder = (BuildContext context) {
      return Column(
        mainAxisSize: MainAxisSize.min, // 设置最小的弹出
        children: <Widget>[
          new ListTile(
            leading: new Icon(Icons.photo_camera),
            title: new Text("Camera"),
            onTap: () async {},
          ),
          new ListTile(
            leading: new Icon(Icons.photo_library),
            title: new Text("Gallery"),
            onTap: () async {},
          ),
        ],
      );
    };
    showModalBottomSheet(context: context, builder: builder);
  }

  bool opened = false;
  void _showBottomSheet(BuildContext context) {
    opened = !opened;
    opened
        ? Scaffold.of(context).showBottomSheet((_) => _buildBottomSheet())
        : Navigator.of(context).pop();
    // Scaffold.of(context).showBottomSheet((_) => _buildBottomSheet());
  }

  Widget _buildBottomSheet() => BottomSheet(
      enableDrag: true,
      elevation: 0,
      // shape: RoundedRectangleBorder(
      //     borderRadius: BorderRadius.only(
      //   topRight: Radius.circular(60),
      //   topLeft: Radius.circular(60),
      // )),
      backgroundColor: Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(0.8),
      onClosing: () => print('onClosing'),
      builder: (_) => (Container(
            height: 250,
            // decoration: BoxDecoration(
            //     image: DecorationImage(
            //         image: AssetImage('assets/images/sabar_bar.jpg'),
            //         fit: BoxFit.cover),
            //     borderRadius: BorderRadius.only(
            //       topRight: Radius.circular(60),
            //       topLeft: Radius.circular(60),
            //     )),
            child: Center(
              child: Text('AAAA'),
            ),
          )));

  // 弹出一个弹窗菜单
  var items = <String>["AA", "BB", "CC", "DD", "FF"];
  List<PopupMenuEntry<String>> _getItemBuilder2() {
    return <PopupMenuEntry<String>>[
      PopupMenuItem<String>(
        value: "1",
        child: ListTile(
          leading: Icon(Icons.share),
          title: Text('分享'),
        ),
      ),
      PopupMenuDivider(), //分割线
      PopupMenuItem<String>(
        value: "2",
        child: ListTile(
          leading: Icon(Icons.settings),
          title: Text('设置'),
        ),
      ),
    ];
  }

  Widget _buildPopupMenu() => PopupMenuButton<String>(
        itemBuilder: (BuildContext context) {
          return _getItemBuilder2();
        },
        icon: Icon(Icons.access_alarm),
        onSelected: (value) {
          print(value);
        },
        onCanceled: () {},
        offset: Offset(200, 100),
      );

  void _showCupertinoDialog() {
    var dialog = CupertinoAlertDialog(
      content: Text(
        "你好,我是你苹果爸爸的界面",
        style: TextStyle(fontSize: 20),
      ),
      actions: <Widget>[
        CupertinoButton(
          child: Text("取消"),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        CupertinoButton(
          child: Text("确定"),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ],
    );

    showDialog(context: context, builder: (_) => dialog);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[_buildPopupMenu()],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FlatButton(
              child: Text('showModalBottomSheet'),
              onPressed: _showModalBottomSheet,
            ),
            Builder(
              builder: (context) => FlatButton(
                  color: Colors.blue,
                  onPressed: () {
                    _showBottomSheet(context);
                  },
                  child: Text(
                    '点我显隐BottomSheet',
                    style: TextStyle(color: Colors.white),
                  )),
            ),
            _buildPopupMenu(),
            FlatButton(
              child: Text('dialog'),
              onPressed: _showCupertinoDialog,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: null,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
